<div class="filter">
    <div class="f1 item" style="width: 140px;">
        <input type="text" class="am-form-field" placeholder="代理账号" />
    </div>
    <div class="f5 item" style="width: 120px;">
        <select data-am-selected>
            <option value="-1">全部状态</option>
            <option value="0">未结算</option>
            <option value="1">已累积</option>
            <option value="2">已结算</option>
            <option value="3">已清除</option>
        </select>
    </div>
    <div class="f9 item" style="width: 120px;">
        <input type="text" class="am-form-field" placeholder="起始月份" id="start"
            data-am-datepicker="{theme:'success'}" readonly required />
    </div>
    <div class="f10 item" style="width: 120px;">
        <input type="text" class="am-form-field" placeholder="结束月份" id="end"
            data-am-datepicker="{theme:'success'}" readonly required />
    </div>

    <div class="button item">
        <button type="button" class="am-btn am-btn-primary">查看</button>
    </div>
    <div class="f20 item">
        <button type="button" class="am-btn am-btn-primary">刷新统计</button>
    </div>
    <div class="clear"></div>
</div>

<!-- 代理模式1 -->
<div style="line-height: 32px; margin-top: 10px; background-color: #65f2fc; color: #000; text-indent: 5px;">
    代理模式：赔率差
</div>
<div class="red">
        佣金 = 赔率差 + 返水差 - 奖励总额
    </div>
<table class="am-table am-table-bordered am-table-radius am-table-striped am-table-compact mode1" id="result-table" 
    style="min-width: 800px;">
    <thead>
    <tr>
        <th>月份</th>
        <th>代理账号</th>
        <th>姓名</th>
        <th>有效会员</th>
        <th>达标用户</th>
        <th>赔率差额</th>
        <th>返水差额</th>
        <th>行政费用</th>
        <th>风险费用</th>
        <th>奖励总额</th>
        <th>本月佣金</th>
        <th>累积佣金</th>
        <th>状态</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    </tbody>
</table>

<!-- 代理模式2 -->
<div style="line-height: 32px; margin-top: 10px; background-color: #65f2fc; color: #000; text-indent: 5px;">
    代理模式：盈利抽成
</div>
<div class="red">
    会员盈利总额 = 用户注单亏损 - 用户注单返水 （负数表示会员有盈利）<br>
    佣金 = 会员盈利总额 * 分红比例 - 风险费用 - 管理费用 - 奖励总额
</div>
<table class="am-table am-table-bordered am-table-radius am-table-striped am-table-compact mode2" id="result-table" 
    style="min-width: 800px;">
    <thead>
    <tr>
        <th>月份</th>
        <th>代理账号</th>
        <th>姓名</th>
        <th>有效会员</th>
        <th>达标用户</th>
        <th>会员盈利总额</th>
        <th>抽成百分比</th>
        <th>行政费用</th>
        <th>风险费用</th>
        <th>奖励总额</th>
        <th>本月佣金</th>
        <th>累积佣金</th>
        <th>状态</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<div class="zxf_pagediv"></div>

<script>
    $(".f9 input").datepicker({format: 'yyyy-mm'});
    $(".f10 input").datepicker({format: 'yyyy-mm'});
    Util.restorePageFilter();

    var _f = Util.queryTemplate($("#result-table tbody"), ".zxf_pagediv", $(".button button"), function(page, size, cb) {
        var uid = $(".f1 input").val().trim();
        var state = $(".f5 select").val();
        var start = $(".f9 input").val();
        var end = $(".f10 input").val();
        var data = {
            pageSize: size,
            pageNum: page,
        };
        if (uid) data.uid = uid;
        if (start) data.start = start;
        if (end) data.end = end;
        if (state != -1) data.state = state;

        Util.savePageFilter([".f1 input", ".f5 select", ".f9 input", ".f10 input"]);
        startLoading();
        CMD.stat_referee_query(data, function(r) {
            stopLoading();
            cb(r);
        });
    }, function(table, list, sum) {
        var table1 = $(".mode1 tbody"), table2 = $(".mode2 tbody");
        if (Util.isEmpty(list)) {
            table.append("<tr><td colspan='14'>暂无数据</tr>");
            return;
        }

        for (var i in list) {
            var o = list[i];
            var tr = $("<tr></tr>");
            tr.append("<td>" + o.month + "</td>");
            tr.append("<td>" + o.user.mobile + "</td>");
            tr.append("<td>" + (o.user.name || '') + "</td>");
            tr.append("<td>" + o.valid + "</td>");
            tr.append("<td>" + o.standard + "</td>");
            var t = table1;
            if (o.type == 1) {
                // 模式1
                tr.append("<td>" + o.profit/100 + "</td>");
                tr.append("<td>" + o.back/100 + "</td>");
            }
            else {
                t = table2;

                // 会员盈利总额
                var s = "_0 - _1 = _4";
                s = s.replace(/_0/, o.profit/100);
                s = s.replace(/_1/, o.back/100);
                s = s.replace(/_4/, (o.profit - o.back)/100);
                tr.append("<td style='text-decoration: underline;'>" + s + "</td>");
                tr.append("<td>" + (o.bonusRate * 100) + "%</td>");
            }

            // 行政费用
            var s = (o.adminFee/100 + "×" + (o.adminRate*100) + "%=") + (o.adminFee * o.adminRate / 100);
            tr.append("<td style='text-decoration: underline;'>" + s + "</td>");

            // 风险费用
            var s = (o.riskFee/100 + "×" + (o.riskRate*100) + "%=") + (o.riskFee * o.riskRate / 100);
            tr.append("<td style='text-decoration: underline;'>" + s + "</td>");

            tr.append("<td>" + o.reward/100 + "</td>");
            tr.append("<td>" + o.commission/100 + "</td>");
            tr.append("<td>" + o.accumulate/100 + "</td>");

            // 状态
            var s = "";
            if (o.state == 0) {
                s = "未结算";
            }
            else if (o.state == 1) {
                s = "已累积";
            }
            else if (o.state == 2) {
                s = "已结算";
            }
            else if (o.state == 3) {
                s = "已清除";
            }
            if (o.check == 1) {
                s += "/待审核"
            }
            else if (o.check == 2) {
                s += "/审核通过";
            }
            tr.append("<td>" + s + "</td>");

            // 操作
            var oper = '';
            if (o.state == 0) 
                var oper = '<button value="__id__" class="accu">累积</button>\
                    <button value="__id__" class="clear">清除</button>\
                    <button value="__id__" class="settle">结算</button>';
            else {
                var oper = '<button value="__id__" class="debug">DEBUG</button>';
            }
            oper = oper.replace(/__id__/g, o.id);
            tr.append("<td>" + oper + "</td>");
            t.append(tr);
        }

        $("button.accu").click(function(e) {
            var id = $(e.currentTarget).attr("value");
            if (!confirm("确定请求累积此单据？")) return;
            startLoading();
            CMD.stat_referee_accumulate(id, null, function() {
                stopLoading();
                _f();
            });
        })
        $("button.settle").click(function(e) {
            var id = $(e.currentTarget).attr("value");
            if (!confirm("确定请求结算此单据？请确保代理的结算条件已经达成后再提交！")) return;
            startLoading();
            CMD.stat_referee_settle(id, null, function() {
                stopLoading();
                _f();
            });
        })
        $("button.clear").click(function(e) {
            var id = $(e.currentTarget).attr("value");
            if (!confirm("确定请求将单据的佣金清零？")) return;
            startLoading();
            CMD.stat_referee_clear(id, null, function() {
                stopLoading();
                _f();
            });
        })
        $("button.debug").click(function(e) {
            var id = $(e.currentTarget).attr("value");
            startLoading();
            CMD.stat_referee_zero(id, null, function() {
                CMD.stat_referee_stat(function() {
                    stopLoading();
                    _f();
                })
            });
        })
    });

    
    $(".f20 button").click(function() {
        if (!confirm("为了避免数据库压力，报表默认一天统计一次。本操作将实时更新统计数据，是否进行此操作？")) return;
        startLoading();
        CMD.stat_referee_stat(function() {
            stopLoading();
            _f();
        })
    });
</script>